
**************************************************************************************************************
/*
THIS DO-FILE:

1. Get PSID wealth and merge with PSID full sample
2. Link parents and children
3. Output data for kid asset imputation ("kid_assets_PSIDimputation.dta")

NOTES:

Wealth files:
- We will use the supplemental files (not the family files) to get the wealth data because the supplemental files are updated 
  versions of the data in the family files. For example, they have imputations for the individuals that did not answer 
  an exact dollar amount.
*/

*************************************************************************************************************

clear all
set more off
set mem 900m
set matsize 4000
set maxvar 10000

*************************************************************************************************************
* 1. Clean supplemental wealth data

use "$rawPSIDdata/wlth1999.dta", clear
keep  S400 S401 S402 S403 S404 S405 S406 S407 S408 S409 S410 S411 S413 S414 S415 S420 S416 S417 S418 S419
rename S400 releasenmbr
rename S401 id
rename S402 business_wtr
rename S403 business_value
rename S404 transacc_wtr
rename S405 transacc_value
rename S406 debt_wtr
rename S407 debt_value
rename S408 realstate_wtr
rename S409 realstate_value
rename S410 stocks_wtr
rename S411 stocks_value
rename S413 vehicles_value
rename S414 otherassets_wtr
rename S415 otherassets_value
rename S420 homeequity_value
rename S416 wealth1
rename S417 wealth2
rename S418 ira_wtr
rename S419 ira_value
gen year=1998
tempfile wealth1998
save `wealth1998',replace

use "$rawPSIDdata/wlth2001.dta", clear
keep  S500 S501 S502 S503 S504 S505 S506 S507 S508 S509 S510 S511 S513 S514 S515 S520 S516 S517 S518 S519
rename S500 releasenmbr
rename S501 id
rename S502 business_wtr
rename S503 business_value
rename S504 transacc_wtr
rename S505 transacc_value
rename S506 debt_wtr
rename S507 debt_value
rename S508 realstate_wtr
rename S509 realstate_value
rename S510 stocks_wtr
rename S511 stocks_value
rename S513 vehicles_value
rename S514 otherassets_wtr
rename S515 otherassets_value
rename S520 homeequity_value
rename S516 wealth1
rename S517 wealth2
rename S518 ira_wtr
rename S519 ira_value
gen year=2000
tempfile wealth2000
save `wealth2000',replace

use "$rawPSIDdata/wlth2003.dta", clear
keep  S600 S601 S602 S603 S604 S605 S606 S607 S608 S609 S610 S611 S613 S614 S615 S620 S616 S617 S618 S619
rename S600 releasenmbr
rename S601 id
rename S602 business_wtr
rename S603 business_value
rename S604 transacc_wtr
rename S605 transacc_value
rename S606 debt_wtr
rename S607 debt_value
rename S608 realstate_wtr
rename S609 realstate_value
rename S610 stocks_wtr
rename S611 stocks_value
rename S613 vehicles_value
rename S614 otherassets_wtr
rename S615 otherassets_value
rename S620 homeequity_value
rename S616 wealth1
rename S617 wealth2
rename S618 ira_wtr
rename S619 ira_value
gen year=2002
tempfile wealth2002
save `wealth2002',replace

use "$rawPSIDdata/wlth2005.dta", clear
keep  S700 S701 S702 S703 S704 S705 S706 S707 S708 S709 S710 S711 S713 S714 S715 S720 S716 S717 S718 S719
rename S700 releasenmbr
rename S701 id
rename S702 business_wtr
rename S703 business_value
rename S704 transacc_wtr
rename S705 transacc_value
rename S706 debt_wtr
rename S707 debt_value
rename S708 realstate_wtr
rename S709 realstate_value
rename S710 stocks_wtr
rename S711 stocks_value
rename S713 vehicles_value
rename S714 otherassets_wtr
rename S715 otherassets_value
rename S720 homeequity_value
rename S716 wealth1
rename S717 wealth2
rename S718 ira_wtr
rename S719 ira_value
gen year=2004
tempfile wealth2004
save `wealth2004',replace

use "$rawPSIDdata/wlth2007.dta", clear
keep  S800 S801 S802 S803 S804 S805 S806 S807 S808 S809 S810 S811 S813 S814 S815 S820 S816 S817 S818 S819
rename S800 releasenmbr
rename S801 id
rename S802 business_wtr
rename S803 business_value
rename S804 transacc_wtr
rename S805 transacc_value
rename S806 debt_wtr
rename S807 debt_value
rename S808 realstate_wtr
rename S809 realstate_value
rename S810 stocks_wtr
rename S811 stocks_value
rename S813 vehicles_value
rename S814 otherassets_wtr
rename S815 otherassets_value
rename S820 homeequity_value
rename S816 wealth1
rename S817 wealth2
rename S818 ira_wtr
rename S819 ira_value
gen year=2006
tempfile wealth2006
save `wealth2006',replace

*append wealth files together
use `wealth1998', clear
append using `wealth2000'
append using `wealth2002'
append using `wealth2004'
append using `wealth2006'
sort id year
save "PSIDwealth.dta", replace
*************************************************************************************************************


*************************************************************************************************************
* 2. Prepare family level file: link parents and children

*get IDs of parents
use "$rawPSIDdata/pid19.dta", clear
rename PID2 IDorigfam
rename PID3 IDperson
rename PID4 IDfam_bmom
rename PID5 IDper_bmom
rename PID6 IDfam_amom
rename PID7 IDper_amom
rename PID23 IDfam_bdad
rename PID24 IDper_bdad
rename PID25 IDfam_adad
rename PID26 IDper_adad
gen person=IDorigfam*1000+IDperson
gen ID_bmom=IDfam_bmom*1000+IDper_bmom
gen ID_amom=IDfam_amom*1000+IDper_amom
gen ID_bdad=IDfam_bdad*1000+IDper_bdad
gen ID_adad=IDfam_adad*1000+IDper_adad
gen IDmom=ID_bmom
replace IDmom=ID_amom if (IDmom==0 | IDmom==.)
gen IDdad=ID_bdad
replace IDdad=ID_adad if (IDdad==0 | IDdad==.)
keep ID* person
sort person
tempfile tempparentID
save `tempparentID', replace

*merge parent IDs to dataset
use "PSID_fulldata.dta", clear
merge m:1 id year using "PSIDwealth.dta", gen(mergewealth) update

*create parent dataset with parent variables used in matching
preserve
foreach var in age inctot marit {
	rename `var' p`var'
}
rename wealth2 passets
gen IDmom=person
gen IDdad=person

keep page passets pinctot pmarit IDmom IDdad year
tempfile tempparentinfo
save `tempparentinfo', replace
restore

*merge parent IDs and info onto main data
merge m:1 person using `tempparentID', keep(master match) gen(mergepID)
merge m:1 IDmom year using `tempparentinfo', keep(master match) gen(mergepinfo)

*parent 'potential age' (for after they leave the sample)
sort person year
foreach year in 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
	bys person: replace page=page[_n-1]+2 if page==.
}
  
*demographics - sex, educ, age are individual-specific, others are HH level
gen kincbin1=(inctot<10000) if inctot!=.
gen kincbin2=(inctot>=10000 & inctot<35000) if inctot!=.
gen kincbin3=(inctot>=35000 & inctot<70000) if inctot!=.
gen kincbin4=(inctot>=35000) if inctot!=.
gen kincbin5=(inctot>=70000) if inctot!=.
gen keducbin=1 if inlist(educ,1,2,3,4,5,6,7,8,9,10,11)
replace keducbin=2 if educ==12 
replace keducbin=3 if inlist(educ,13,14,15,16,17)
gen kmale=(sex==1) if inlist(sex,1,2)
gen kchildren=kids
replace kchildren=5 if inlist(kchildren,5,6,7,8,9)
gen kmarried=1 if marit==1
replace kmarried=0 if inlist(marit,2,3,4,5)
gen kownhome=(house>0) if house!=. & house!=9999998 & house!=9999999 
rename age kage
rename wealth2 kassets

scalar cpi1992=134.250 //1991
scalar cpi1994=142.058 //1993
scalar cpi1996=149.758 //1995
scalar cpi1998=157.558 //1997
scalar cpi2000=163.233 //1999
scalar cpi2002=173.517 //2001
scalar cpi2004=179.808 //2003
scalar cpi2006=190.975 //2005
scalar cpi2008=202.767 //2007
scalar cpi2010=209.630 //2009
scalar cpi2012=221.575 //2011
scalar cpi2014=229.324 //2013
scalar cpi2010actual=213.967 //2010

foreach yr in 1998 2000 2002 2004 2006 2008 2010 2012 2014 {
	replace kassets=kassets*cpi2010actual/cpi`yr' if year==`yr'
	replace passets=passets*cpi2010actual/cpi`yr' if year==`yr'
	replace pinctot=pinctot*cpi2010actual/cpi`yr' if year==`yr'
}

*psample: 
*	1=parent alive and matched, 65ish and single
*	2=observation after a "1"
*	0=parent doesn't have a "1" ever
*	.=no parent matched
gen psample=(page>=65 & inlist(pmarit,2,3,4)) if page!=. & pmarit!=.
sort person year
bys person: egen inpsamptemp=max(psample)
replace psample=2 if inpsamptemp==1 & psample!=1 & (page>=65 | page==.) & (inlist(pmarit,2,3,4) | pmarit==.)
drop inpsamptemp

keep if inlist(psample,1,2) & year>=1998
*************************************************************************************************************


*************************************************************************************************************
*3. OUTPUT DATA FOR INITIAL CONDITIONS:
keep if kassets!=. & keducbin!=. & kmale!=. & kchildren!=. & kmarried!=. & kownhome!=. & kage!=. & kincbin1!=.
keep if passets!=. & pinctot!=. & page!=. 

gen PSIDsample=1
keep PSIDsample kassets kincbin* keducbin kmale kchildren kmarried kownhome kage passets pinctot page year
save "kid_assets_PSIDimputation.dta", replace

*************************************************************************************************************
